#include "oPanel.h"

#include "notify_adaptor.h"
#include "notify_interface.h"


oPanel::oPanel(QWidget * parent) : QWidget(parent) {
    setGeometry(0,0,480,38);
    this->setWindowTitle("oPanel");
    this->setWindowFlags( Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

    notify = new QNotification(this);
    notify->setGeometry(0,5,400,90);
    notify->show();

    new NotifyAdaptor(this);
    QDBusConnection::sessionBus().registerObject("/", this);
    com::opendesk::notify *iface;
    iface = new com::opendesk::notify(QString(), QString(),QDBusConnection::sessionBus(),this);

    connect(iface, SIGNAL(message(QString,QString)), this, SLOT(newNotify()));
    connect(iface, SIGNAL(call(QString)), this, SLOT(newNotify()));

    down= false;
}

oPanel::~oPanel(){
    for (int i =0 ; i< listWidget.size() ; i++) {
        delete listWidget.at(i);
    }
    listWidget.clear();
}

void oPanel::addWidget(QString w) {
   int x;
   x = 480;

   for (int i =0; i<listWidget.size(); i++) {
        x = x - listWidget.at(i)->width();
   }

   if (w == "clock" ) {
            QDigitalClock * clock = new QDigitalClock(this);
            x = x-clock->width()-3;
            clock->setGeometry(x,0,clock->width(),clock->height());
            listWidget.push_back(clock);
   }

   if (w == "battery-monitor" ) {
            QBattery * battery = new QBattery(this);
            x = x-battery->width()-3;
            battery->setGeometry(x,0,battery->width(), battery->height());
            listWidget.push_back(battery);
    }

}


void oPanel::paintEvent(QPaintEvent * event){
    QPainter p(this);

    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);

    //compositing
    /*
    p.save();
    p.setCompositionMode(QPainter::CompositionMode_Source);
    p.fillRect(rect(), Qt::transparent);
    p.restore();

    p.setPen(Qt::NoPen);
    p.setBrush(Qt::transparent);

    p.drawRect(0,0, width(), height());
*/
    QColor darkBlack(16,16,16,255);
    QRect Bar(0,0,width(),height());//-14);
    p.setPen(darkBlack);
    p.setBrush(darkBlack);

    p.save();
    p.drawRect(Bar);
    p.restore();

    //Angle for compositing option
    /*
    p.save();
    for (int i=0; i<30*5; i++) {
        p.drawArc(-27+i/5,height()-14,20,20,90*16,90*16);
    }
    p.restore();

    p.save();
    for (int i=0; i<30*5; i++) {
        p.drawArc(487-i/5,height()-14,20,20,0*16,90*16);
    }
    p.restore();
    */
    p.end();
}



void oPanel::resizeEvent(QResizeEvent * event) {
    //set the window dimension and form
    //if compositing enable :
    /*QPolygon maskPolygon(8);
    maskPolygon.setPoint(0,0,0);
    maskPolygon.setPoint(1,480,0);
    maskPolygon.setPoint(2,480,height());
    maskPolygon.setPoint(3,477,height());
    maskPolygon.setPoint(4,464,height()-13);
    maskPolygon.setPoint(5,16,height()-13);
    maskPolygon.setPoint(6,3,height());
    maskPolygon.setPoint(7,0,height());
    maskPolygon.end();
    QRegion maskRegion(maskPolygon);*/
    //else:
    QList <point> list;
    point a;
    a.x=9;
    a.y=0;
    list.push_back(a);
    a.x=8;
    a.y=0;
    list.push_back(a);
    a.x=8;
    a.y=2;
    list.push_back(a);
    a.x=7;
    a.y=4;
    list.push_back(a);
    a.x=6;
    a.y=5;
    list.push_back(a);
    a.x=5;
    a.y=6;
    list.push_back(a);
    a.x=4;
    a.y=7;
    list.push_back(a);
    a.x=2;
    a.y=7;
    list.push_back(a);
    a.x=0;
    a.y=8;
    list.push_back(a);
    a.x=0;
    a.y=9;
    list.push_back(a);
    /*double x,y, Pi=3.141592653589793;
    list.clear();
    for (int i =1; i<=Pi/2*13*10000;i++){
        x=13*sin(i/10000+((Pi)*13)/10000);
        y=13*cos(i/10000+((Pi)*13)/10000);
        a.x=x;
        a.y=y;
        list.push_back(a);
    }*/
    QPolygon maskPolygon(7+list.size()*2);
    maskPolygon.setPoint(0,0,0);
    maskPolygon.setPoint(1,480,0);
    maskPolygon.setPoint(2,480,height());
    maskPolygon.setPoint(3,477,height());
    for (int i=0;i<list.size();i++){
        maskPolygon.setPoint(3+(i+1),477+(list.at(i).x-9),height()-(4+list.at(i).y));
    }
    maskPolygon.setPoint(4+list.size(),12,height()-12);
    for (int i=list.size()-1;i>=0;i--){
        maskPolygon.setPoint(4+list.size()+(list.size()-i),-list.at(i).x+12,height()-(4+list.at(i).y));
        QString why= QString::number(-list.at(i).x+12)+" "+QString::number((4+list.at(i).y));
        qWarning(why.toStdString().c_str());
    }
    maskPolygon.setPoint(5+list.size()*2,3,height());
    maskPolygon.setPoint(6+list.size()*2,0,height());
    maskPolygon.end();
  /*  float  x,y,pi = 3.141592653589793;
    int r=13, p= pi/2*13;
    QPolygon maskPolygon(3+p*2);
    //X=r*sen(teta) , Y=r*cos(teta)
    maskPolygon.setPoint(0,0,0);
    maskPolygon.setPoint(1,480,0);
    maskPolygon.setPoint(2,480,height());
    maskPolygon.setPoint(3,477,height());
    for (int i =1; i<=p;i++){
        x=r*sin(i-1);
        y=r*cos(i-1);
        maskPolygon.setPoint(3+i,477-(r-x),height()-y);
    }
    maskPolygon.setPoint(4+p, 16,height()-13);
    for (int i =1; i<=p;i++){
        x=r*sin(i-1+pi/2);
        y=r*cos(i-1+pi/2);
        maskPolygon.setPoint(3+i,16+x,height()-13+y);
    }
    maskPolygon.setPoint(p*2+5,0,height());
    maskPolygon.end();
*/
    QRegion maskRegion(maskPolygon);
    this->setMask(maskRegion);
}


void oPanel::showNotify(){
    if (!down) {
        tanimation = new QTimer();
        connect(tanimation, SIGNAL(timeout()), this, SLOT(animationDown()));
        tanimation->start(10);
    }
    else {
        tanimation = new QTimer();
        connect(tanimation, SIGNAL(timeout()), this , SLOT(animationUp()));
        tanimation->start(10);
    }
}

void oPanel::animationDown(){
    if (height()<111) {
        setGeometry(x(), y(), width(), height()+2);
        //update();
    }
    else {
        down = true;
        tanimation->stop();
        delete tanimation;
    }
}

void oPanel::animationUp(){
    if (height()>38) {
        setGeometry(x(), y(), width(), height()-2);
        update();
    }
   /* else {
        down = false;
        tanimation->stop();
        delete tanimation;
    }*/
}

void oPanel::newNotify(){
    showNotify();
    //setGeometry(x(),y(), width(), 111);
   // down = true;
}

void oPanel::mousePressEvent(QMouseEvent * event){
    if (down) {
        setGeometry(x(),y(),width(),38);
        down = false;
    }
    else {
        down = true;
        setGeometry(x(),y(),width(),111);
    }
}

